///
///Author: YYF
///Date: 2020-10-21 09:20:15
///Description:
///

///json转换辅助工厂，把json转为T
class EntityFactory {
  static T generateOBJ<T>(json) {
    if (json == null) {
      return null;
    }
//可以在这里加入任何需要并且可以转换的类型，例如下面
//  else if (T.toString() == "LoginEntity") {
//   return LoginEntity.fromJson(json) as T;
//  }
    else {
      return json as T;
    }
  }
}

/// 数据基类，返回的参数为 {“code”: 0, “msg”: “”, “data”: {}}
class BaseEntity<T> {
  String msg;
  T data;
  int status;

  BaseEntity({this.msg, this.data, this.status});

  BaseEntity.fromJson(Map<String, dynamic> json) {
    msg = json['msg'];
    data = json['data'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['msg'] = this.msg;
    data['data'] = this.data;
    data['status'] = this.status;
    return data;
  }
}

/// 数据基类2，返回的参数为 {“code”: 0, “msg”: “”,"total_pages":0,"total_cnt":0, “data”: []}
class BaseListEntity<T> {
  int status;
  String msg;
  int totalPages;
  int totalCnt;
  List<T> data;

  BaseListEntity(
      {this.status, this.msg, this.totalPages, this.totalCnt, this.data});

  factory BaseListEntity.fromJson(json) {
    List<T> mData = List();
    if (json['data'] != null) {
      //遍历data并转换为我们传进来的类型
      (json['data'] as List).forEach((v) {
        mData.add(EntityFactory.generateOBJ<T>(v));
      });
    }
    return BaseListEntity(
      status: json["status"],
      msg: json["msg"],
      totalPages: json["total_pages"],
      totalCnt: json["total_cnt"],
      data: mData,
    );
  }
}

/// 请求报错基类，{“code”: 0, “msg”: “”}
class ErrorEntity {
  int status;
  String msg;
  ErrorEntity({this.status, this.msg});
}
